package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.FilmProduct;
import com.ala4.oxcafe.repository.mapper.FilmProductMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 车膜产品实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class FilmProductRepository extends ServiceImpl<FilmProductMapper, FilmProduct> implements IService<FilmProduct> {

    private LambdaQueryWrapper<FilmProduct> queryParam(FilmProduct filmProduct) {

        LambdaQueryWrapper<FilmProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StrUtil.isNotEmpty(filmProduct.getSearchValue()), item -> {
            item.like(FilmProduct::getSeries, filmProduct.getSearchValue())
                    .or().like(FilmProduct::getNameEn, filmProduct.getSearchValue())
                    .or().like(FilmProduct::getNameCn, filmProduct.getSearchValue());
        });
        queryWrapper.eq(ObjectUtil.isNotNull(filmProduct.getSeries()), FilmProduct::getSeries, filmProduct.getSeries());
        queryWrapper.eq(ObjectUtil.isNotNull(filmProduct.getSpecs()), FilmProduct::getSpecs, filmProduct.getSpecs());
        queryWrapper.eq(ObjectUtil.isNotNull(filmProduct.getType()), FilmProduct::getType, filmProduct.getType());
        queryWrapper.eq(ObjectUtil.isNotNull(filmProduct.getStatus()), FilmProduct::getStatus, filmProduct.getStatus());
        queryWrapper.like(ObjectUtil.isNotNull(filmProduct.getNameCn()), FilmProduct::getNameCn, filmProduct.getNameCn());
        queryWrapper.like(ObjectUtil.isNotNull(filmProduct.getNameEn()), FilmProduct::getNameEn, filmProduct.getNameEn());

        return queryWrapper;
    }

    public IPage<FilmProduct> selectFilmProductPage(PageDomain pageDomain, FilmProduct filmProduct) {
        return this.page(pageDomain.toPlusPage(), queryParam(filmProduct));
    }

    public List<FilmProduct> selectFilmProductList(FilmProduct filmProduct) {
        return this.list(queryParam(filmProduct));
    }

    public Map<Long, FilmProduct> listToMap(List<Long> productIds) {
        List<FilmProduct> list = this.list(Wrappers.<FilmProduct>lambdaQuery().in(FilmProduct::getId, productIds));
        return list.stream().collect(Collectors.toMap(FilmProduct::getId, Function.identity()));
    }
}
